package com.macro.mall.controller;

import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.Common;
import com.macro.mall.service.CommonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author super
 * @title: CommonController
 * @description: TODO
 * @date 2020/8/15 14:16
 */
@Slf4j
@RestController
@RequestMapping("common")
@Api(tags = "common", description = "字典表(common)")
public class CommonController {

    @Autowired
    private CommonService commonService;

    @ApiOperation("查询字典表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult list(Common common) {
        List<Common> commons = commonService.find(common);
        return CommonResult.success(commons);
    }


    @ApiOperation("添加字典数据")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult save(@RequestBody Common request) {
        try {
            commonService.saveNotNull(request);
            return CommonResult.success(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.failed();

    }

    @ApiOperation("获取字典表key")
    @RequestMapping(value = "/getKay", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getKay() {
        try {
            List<Common> list = commonService.getKay();
            return CommonResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.failed();
    }
    @ApiOperation("查询字典详情")
    @RequestMapping(value = "/getById", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Common> getById(@ApiParam(value = "id") @RequestParam(value = "id", required = true) Integer id) {
        try {
            Common one = commonService.findOne(id);
            return CommonResult.success(one);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.failed();
    }


    @ApiOperation("删除字典信息")
    @RequestMapping(value = "/delById", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult deleteById(@ApiParam(value = "id") @RequestParam(value = "id", required = true) Integer id) {
        try {
            commonService.deleteById(id);
            return CommonResult.success(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.failed();

    }

    @ApiOperation("更新字典信息")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@ApiParam(value = "请求报文") @RequestBody Common request) {
        try {
            commonService.updateNotNullById(request);
            return CommonResult.success(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.failed();

    }



}
